import {
	postJson,
	get
} from '@/utils/request.js'
const needRefreshList = ['pages/mall/confirm', 'pages/mall/cart', 'pages/user/info', 'pages/index/user',
	'pages/user/address', 'pages/order/index', 'pages/order/detail'
]
export default {
	methods: {
		login(backUrl) {
			this.$utils.loading();
			uni.login({
				success: res => {
					console.log(res)
					uni.getUserInfo({
						success: res1 => {
							postJson('/api/login', {
								code: res.code,
								userInfo: res1.userInfo,
								needToken: false,
							}).then(res2 => {
								uni.hideLoading()
								if (res2.access_token) {
									uni.setStorageSync('token',
										`${res2.token_type} ${res2.access_token}`);
									this.$store.commit('setToken',`${res2.token_type} ${res2.access_token}`)
								}
								uni.showToast({
									icon: 'none',
									title: '登录成功'
								})
								const prePage = getCurrentPages()[getCurrentPages()
									.length - 2];
								if (!getApp().globalData.noNeedRefresh || (prePage &&
										needRefreshList.includes(prePage.route))) {
									getApp().globalData.refresh = true;
								}
								getApp().globalData.noNeedRefresh = false;

								setTimeout(() => {
									if (res2.is_info == 1) {
										return uni.redirectTo({
											url: '/pages/user/info?first=1'
										})
									}
									get('/api/user/info', {}, false).then(
										res => {
											uni.setStorageSync('userInfo',
												res);
											getApp().globalData.role = res
												.role;
											if (backUrl) {
												return uni.redirectTo({
													url: backUrl
												})
											}
											if (getCurrentPages().length ==
												1) {
												return uni.reLaunch({
													url: '/pages/index/index'
												})
											}
											uni.navigateBack({
												delta: '1'
											})
										}).catch(err => {
										console.log(err)
										if (backUrl) {
											return uni.redirectTo({
												url: backUrl
											})
										}
										if (getCurrentPages().length ==
											1) {
											return uni.reLaunch({
												url: '/pages/index/index'
											})
										}
										uni.navigateBack({
											delta: '1'
										})
									}).finally(() => {})
								}, 1000)
							}).catch(() =>
								uni.hideLoading())
						}
					})
				},
				fail: err => {
					uni.hideLoading()
					console.log(err)
				}
			});
		},
		checkAuth(noNeedRefresh) {
			console.log('checkAuth', noNeedRefresh)
			if (noNeedRefresh) {
				getApp().globalData.noNeedRefresh = noNeedRefresh;
			}
			return new Promise((resolve, reject) => {
				if (!uni.getStorageSync('token')) {
					uni.navigateTo({
						url: '/pages/user/login'
					})
					return reject();
				}
				return resolve();
			})

		},
		handleToCart() {
			this.checkAuth(true).then(() => {
				uni.navigateTo({
					url: '/pages/mall/cart'
				})
			})
		},
	}
}